Ext.onReady(function() {
    var psize = 18;
    var store;
    var nodeid;
    var jReader1;
    var tree_root = new Ext.tree.AsyncTreeNode( {
        text : '指标分类',
        id : '0'
    });
    
    var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.report.shuju_tianbao';
    openUrl += '&methodName=getDemeGrid&paramCount=10';
    jReader1 = new Ext.data.JsonReader( {
        root: 'data',
        totalProperty: 'totalCount',
        fields : [ {
            name : 'repid'
        }, {
            name : 'repname'
        }, {
            name : 'repcode'
        }, {
            name : 'cataid'
        }, {
            name : 'repdesc'
        }, {
            name : 'desc'
        }, {
            name : 'dataprec'
        }, {
            name : 'dataunit'
        }, {
            name : 'department'
        }, {
            name : 'departmentid'
        }, {
            name : 'sourcetype'
        }, {
            name : 'datatype'
        }, {
            name : 'updatetime'
        }, {
            name : 'reffact'
        }, {
            name : 'repvaluecol'
        } ]
    });
		
    store = new Ext.data.Store( {
        storeId : 'store1',
        proxy : new Ext.data.HttpProxy( {
            url : openUrl
        }),
        reader : jReader1,
        baseParams : {
            param_0:0,
            param_1:psize,
            param_2:"",
            param_3:"",
            param_4:"",
            param_5:"",
            param_6:"",
            param_7:"",
            param_8:roleid,
            param_9:""
        }
    });
		

    var sm = new Ext.grid.CheckboxSelectionModel({
        singleSelect : true,    
    	header:''
    });

    var cm = new Ext.grid.ColumnModel( [ sm, {
        hidden : true,
        header : '指标编号',
        sortable : true,
        dataIndex : 'repid'
    }, {
        header : '指标名称',
        sortable : true,
        dataIndex : 'repname'
    }, {
        header : '指标编码',
        sortable : true,
        dataIndex : 'repcode'
    }, {
        header : '所属分类',
        sortable : true,
        dataIndex : 'cataid'
    }, {
        hidden : true,
        header : '指标含义',
        sortable : true,
        dataIndex : 'repdesc'
    }, {
        hidden : true,
        header : '算法描述',
        sortable : true,
        dataIndex : 'desc'
    }, {
        header : '数据精度',
        sortable : true,
        dataIndex : 'dataprec'
    }, {
        header : '数据单位',
        sortable : true,
        dataIndex : 'dataunit'
    }, {
        header : '归口部门',
        sortable : true,
        dataIndex : 'department'
    }, {
        hidden:true,
        header : '归口部门ID',
        sortable : true,
        dataIndex : 'departmentid'
    }, {
        header : '数据来源类型',
        sortable : true,
        dataIndex : 'sourcetype',
        renderer : function(v){
            if(v==1) {
                return "自动抽取 ";
            }else if(v==2) {
                return "手工填报";
            }
        }
    }, {
        hidden:true,
        header : '数据口径',
        sortable : true,
        dataIndex : 'datatype',
        renderer : function(v){
            if (v == 1) {
                return "全口径";
            } else if (v == 2) {
                return "国网";
            } else if (v == 3) {
                return "省网";
            } else if (v == 4) {
                return "上报国网";
            } else if (v == 5) {
                return "统调口径";
            }
        }
    }, {
        hidden : true,
        header : '更新周期',
        sortable : true,
        dataIndex : 'updatetime'
    }, {
        hidden : true,
        header : '关联事实表',
        sortable : true,
        dataIndex : 'reffact'
    }, {
        hidden : true,
        header : '指标值字段',
        sortable : true,
        dataIndex : 'repvaluecol'
    } ]);

    var grid = new Ext.grid.GridPanel( {
        id : 'xxx',
        anchor:'100% -111',
        store : store,
        viewConfig:{
            forceFit: true
        },
        sm : sm,
        cm : cm,
        frame : true,
        columnLines : true,
        loadMask:true,//数据加载时的遮罩功能 
        height : screen.height - 412,
        stateful : true,
        stateId : 'grid',
        bbar: new Ext.PagingToolbar({
            store: store,
            paramNames : {
                start : 'param_0',
                limit : 'param_1'
            }, 
            emptyMsg : '没有数据需要显示',
            displayInfo: true,
            pageSize: psize,
            listeners:{
                beforechange:function(bar,params){
                    bar.store.setBaseParam("param_2",Ext.ComponentMgr.get("mingcheng").getValue());
                    bar.store.setBaseParam("param_3",Ext.ComponentMgr.get("bumen").getValue());
                    bar.store.setBaseParam("param_4",Ext.ComponentMgr.get("laiyuan").getValue());
                    bar.store.setBaseParam("param_5",Ext.ComponentMgr.get("koujing").getValue());
                    bar.store.setBaseParam("param_6",Ext.ComponentMgr.get("zhouqi").getValue());
                    bar.store.setBaseParam("param_7",nodeid);
                    bar.store.setBaseParam("param_8",roleid);
                    bar.store.setBaseParam("param_9",Ext.ComponentMgr.get("bianma").getValue());
                }
            }
        })
    });
    store.load();
    var toolbar = new Ext.Toolbar( {
        //    id : 'toolbar',
        stateful : true,
        items : ['-', {
            //      id : 'show-btn',
            text : '数据填报',
            handler : function() {
                var re_grid = Ext.ComponentMgr.get("xxx").getSelectionModel().getSelected();
                if (re_grid){
                    var repid=re_grid.data.repid;//判断     
                    window.showModalDialog("filldata.jsp?repid="+repid, '','dialogWidth=900px;dialogHeight=770px;status:no;resizable:yes;Minimize=yes;Maximize=yes;');
                }else{
                    Ext.Msg.alert('提示','请选择指标');
                }
            }
        },'-',{
            id   : 'tbcanshu',
            text : '参数数据填报',
            handler : function() {
                var re_grid = Ext.ComponentMgr.get("xxx").getSelectionModel().getSelected();
                if (re_grid){
                    var repid=re_grid.data.repid; //判断         
                    Ext.ComponentMgr.get("get_p_id").setValue(repid);
                    show_tbcanshu();
                }else{
                    Ext.Msg.alert('提示','请选择指标');
                }
            }
        },{
            xtype:"hidden",
            id:'get_p_id'
        },'-']
    });

    //			dept_cx_ begin
    var selectedNode_dept_cx = null;
    var comb_dept_cx = new Ext.form.ComboBox(
    {
        id : 'dept_cx',
        store : new Ext.data.SimpleStore( {
            fields : [],
            data : [ [] ]
        }),
        editable : false,
        fieldLabel : '归口部门',
        shadow : false,
        mode : 'local',
        emptyText : '请选择归口部门',
        anchor : '100%',
        triggerAction : 'all',
        tpl : '<tpl for="."><div style="height:200px"><div id="tree_dept_cx"></div></div></tpl>',
        selectedClass : '',
        onSelect : Ext.emptyFn
    });
    comb_dept_cx.on("collapse", function() {
        //				if (selectedNode_dept_cx == null) {
        //					comb_dept_cx.expand();
        //				}
        });
			
    var tree_root_dept_cx = new Ext.tree.AsyncTreeNode( {
        text : '部门',
        id : -1
    });

    var tree_dept_cx = new Ext.tree.TreePanel(
    {
        border : '0',
        height : 200,
        useArrows : true,
        autoScroll : true,
        animate : true,
        enableDD : false,
        containerScroll : true,
        rootVisible : false,
        root : tree_root_dept_cx,
        autoScroll : true,
        lines : false,
        iconCls : 'nav',
        checkModel : 'cascade',
        onlyLeafCheckable : false,
        titleCollapse : true,
        listeners : {
            beforeload : function(node) {
                var openUrl_cx = basePath + 'JarpListener?className=cn.com.jsepc.rep.report.CorpDeptTree&methodName=getCorpDeptTreeData&paramCount=2';
                openUrl_cx += '&param_0='+node.id+'&param_1=';
                node.loader = new Ext.tree.TreeLoader( {
                    dataUrl : openUrl_cx
                });
            }
        }
    });

    tree_dept_cx.on('click', function(node) {
        selectedNode_dept_cx = node;
        var nodename = node.text;
        Ext.ComponentMgr.get("dept_cx").setValue(nodename);
        Ext.ComponentMgr.get("bumen").setValue(node.id);		
        selectedNode_dept_cx = null;
        comb_dept_cx.canCollapse = true;
        comb_dept_cx.collapse();
    });

    comb_dept_cx.on('expand', function() {
        tree_dept_cx.render('tree_dept_cx');
    });

    tree_dept_cx.on('expandnode', function() { comb_dept_cx.canCollapse = true; });
    tree_dept_cx.on('beforeload', function() { comb_dept_cx.canCollapse = false; });
    tree_dept_cx.on('beforeexpandnode', function() { comb_dept_cx.canCollapse = false; });
    tree_dept_cx.on('beforecollapsenode', function() { comb_dept_cx.canCollapse = false; });

     comb_dept_cx.on('expand', function () {
            comb_dept_cx.canCollapse = true;
            if (tree_dept_cx) {
                comb_dept_cx.canCollapse = true;          
                tree_dept_cx.getRootNode().expand();
                
            }
      });
     comb_dept_cx.on('collapse', function() {
            if (!comb_dept_cx.canCollapse) {
                comb_dept_cx.expand();
            }
        });    
    //			dept_cx end

    var viewport = new Ext.Viewport( {
        layout : 'border',
        items : [ {
            region : 'center',
            //frame : true,
            layout : 'border',
            tbar : toolbar,
            items : [ {
                tools : [ {
                    id : 'refresh',
                    handler : function() {
                        if (tree_root != null) {
                            tree_root.reload();
                        }
			
                    }
                } ],
                title:'指标分类',
                collapsible : true,
                split:true,
                region : 'west',
                //     id : 'west-panel',
                //frame : true,
                width : 200,
                minSize : 200,
                maxSize : 200,
                // collapsible : true,
                html : '<div id="tree_tb"></div>'
            }, 
            new Ext.Panel( {
                region : 'center',
                frame : true,
                title : '指标列表',
                deferredRender : false,
                activeTab : 0,
                layout:"anchor",
                // collapsible : true,
                // form : 'fit',
               // html : '<div id="grid_tb"></div>',     
                items:[ new Ext.FormPanel({
        anchor: '-0,-0',
        id : 'p_query',
        layout : 'form',
        labelWidth : 80,
        bodyStyle : 'padding: 0 10px 0 10px',
        border : false,
	        items : [ {
	            title : '查询条件',
	            layout : 'form',
	            xtype : 'fieldset',
	            collapsible : true,
	            collapsed : false,
	            labelAlign : 'right',
	            height : '100%',
	            defaults : {
	                width : '100%'
	            },
	            items : [
		            {
		                layout : 'column',
		                border : false,
		                items : [ {
		                    columnWidth : .3,
		                    layout : 'form',
		                    labelWidth : 80,
		                    border : false,
		                    bodyStyle : 'padding:5px 5px 0',
		                    items : [ {
		                        xtype : "textfield",
		                        fieldLabel : '指标名称',
		                        emptyText : '请填写',
		                        width : '100%',
		                        id : 'mingcheng'
		                    } ]
		                },{
		                    columnWidth : .3,
		                    layout : 'form',
		                    labelWidth : 80,
		                    border : false,
		                    bodyStyle : 'padding:5px 5px 0',
		                    items : [ {
		                        xtype : "textfield",
		                        fieldLabel : '指标编码',
		                        emptyText : '请填写',
		                        width : '100%',
		                        id : 'bianma'
		                    } ]
		                } ,
	                {
	                    columnWidth : .26,
	                    layout : 'form',
	                    labelWidth : 80,
	                    anchor : '80%',
	                    width : '80%',
	                    border : false,
	                    bodyStyle : 'padding:5px 5px 0',
	                    items : [ {
	                        xtype : "combo",
	                        triggerAction : "all",
	                        fieldLabel : "更新周期",
	                        emptyText : '请选择',
	                        blankText : '此项必选',
	                        id : 'zhouqi',
	                        anchor : '100%',
	                        editable : false,
	                        store : [ [ '1', '年' ],
	                        [ '2', '季' ],
	                        [ '3', '月' ],
	                        [ '4', '日' ],
	                        [ '5', '时' ],
	                        [ '6', '分' ],
	                        [ '7', '全部' ] ]
	                    } ]
	                } ]
		            },
	            {
	                layout : 'column',
	                border : false,
	                items : [
	                {
	                    columnWidth : .3,
	                    layout : 'form',
	                    labelWidth : 80,
	                    border : false,
	                    bodyStyle : 'padding:5px 5px 0',
	                    items : [
	                    {
	                        xtype : 'hidden',
	                        id : 'koujing'
	                    },
	                    {
	                        xtype : "combo",
	                        triggerAction : "all",
	                        fieldLabel : "数据来源",
	                        anchor : '100%',
	                        emptyText : '请选择',
	                        id : 'laiyuan',
	                        editable : false,
	                        store : [
	                        [ '1',
	                        '自动抽取' ],
	                        [ '2',
	                        '手工填报' ],
	                        [ '3', '全部' ] ]
	                    } ]
	                }, {
		                    columnWidth : .4,
		                    layout : 'form',
		                    labelWidth : 80,
		                    border : false,
		                    bodyStyle : 'padding:5px 5px 0',
		                    items : [ comb_dept_cx, {
		                        id : 'bumen',
		                        fieldLabel : '归口部门',
		                        xtype : "hidden",
		                        emptyText : '请填写',
		                        width : '80%'
		                    } ]
		                },
	                {
	                    columnWidth : .2,
	                    layout : 'form',
	                    labelWidth : 80,
	                    anchor : '100%',
	                    border : false,
	                    bodyStyle : 'padding:5px 0 5px 80px',
	                    items : [ {
	                        xtype : "button",
	                        id : 'query_tbar',
	                        text : "查  询",
	                        width : 60,
	                        handler : function() {
	                            query();
	                        }
	                    } ]
	                }]
	            } ]
	        } ]
    }),grid]
            }) ]
        } ]
    });
	
	
    var tree = new Ext.tree.TreePanel(
    {
        //     id:'terr_fenlei',
        renderTo : 'tree_tb',
        border : '0',
        height : 900,
        //  width : 200,
        useArrows : true, // 使用箭头
        autoScroll : true,
        animate : true, // 动画
        enableDD : false, // 可拖动
        containerScroll : true,
        //frame:true,
        rootVisible : true,
        root : tree_root,
        autoScroll : true,
        listeners : {
            beforeload : function(node) {
                var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.catalog.CatalogDAO&methodName=getCatalog&paramCount=0'
                node.loader = new Ext.tree.TreeLoader( {
                    dataUrl : openUrl
                });
            }
        }
    });
		
		
    
		
		
    tree.on('click',function(node) {
        nodeid = node.id;
        store.load({
            params : {
                param_2:Ext.ComponentMgr.get("mingcheng").getValue(),
                param_3:Ext.ComponentMgr.get("bumen").getValue(),
                param_4:Ext.ComponentMgr.get("laiyuan").getValue(),
                param_5:Ext.ComponentMgr.get("koujing").getValue(),
                param_6:Ext.ComponentMgr.get("zhouqi").getValue(),
                param_7:nodeid,
                param_8:roleid,
                param_9:Ext.ComponentMgr.get("bianma").getValue()
            }
        });	
        setempty();		
    });
		
		
    function query() {
	
        var keyword_2 = Ext.ComponentMgr.get("mingcheng").getValue();
        var keyword_3 = Ext.ComponentMgr.get("bumen").getValue();
        var keyword_4 = Ext.ComponentMgr.get("laiyuan").getValue();
        var keyword_5 = Ext.ComponentMgr.get("koujing").getValue();
        var keyword_6= Ext.ComponentMgr.get("zhouqi").getValue();	
        var keyword_9= Ext.ComponentMgr.get("bianma").getValue();	

        store.load({
            params : {
                param_2:keyword_2,
                param_3:keyword_3,
                param_4:keyword_4,
                param_5:keyword_5,
                param_6:keyword_6,
                param_7:nodeid,
                param_8:roleid,
                param_9:keyword_9
            }
        });	
    }
 //   grid.render('grid_tb');
		
    function setempty() {
        Ext.ComponentMgr.get("mingcheng").setValue("");
        Ext.ComponentMgr.get("bumen").setValue("");
        Ext.ComponentMgr.get("laiyuan").setValue("");
        Ext.ComponentMgr.get("koujing").setValue("");
        Ext.ComponentMgr.get("zhouqi").setValue("");
        Ext.ComponentMgr.get("bianma").setValue("");
    };
});